package com.hj.service.impl;

import java.util.List;

import com.hj.dao.CompanyMapper;
import com.hj.entity.Ss_accesscode;
import com.hj.service.StopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.hj.dao.StopMapper;
import com.hj.entity.Company;
import com.hj.utils.MultipleDataSource;


@Service("stopService")
public class StopServcieImpl implements StopService {

	private static Logger logger = LoggerFactory.getLogger(StopServcieImpl.class);

	@Autowired
	private StopMapper stopmapper;

	@Autowired
	private CompanyMapper companymapper;

	@Value("${cp.agency.need.ignore}")
	private String agencyNameIgnores;
	
	@Scheduled(cron="0 0/4 * * * ?")
	public void stopJob() {
		String[] agencyIgnores =agencyNameIgnores.split(",");
		//查boss库到续约截止日的用户
		MultipleDataSource.setDataSourceKey("dataSource2");
		List<Company> companyList = stopmapper.search_cp(agencyIgnores);

		//查询所有需停机的400号码接入策略并插入备份表
		MultipleDataSource.setDataSourceKey("dataSource");
		List<Ss_accesscode> accesscodeList = stopmapper.search_needStop(companyList);

		if (accesscodeList.size()>0) {

			MultipleDataSource.setDataSourceKey("dataSource2");
			//修改400号码为停机状态
			companymapper.update_companyStop(companyList);
			//将停机前的接入策略插入到备份表
			stopmapper.insert_strategyRecord(accesscodeList);

			//逐个停机
			MultipleDataSource.setDataSourceKey("dataSource");
			String vccId="";
			for (Ss_accesscode accesscode : accesscodeList) {
				vccId += accesscode.getVccId()+" ";
				stopmapper.update_stop(accesscode.getVccId());
			}

			logger.info("代理商号码停机列表： {}", vccId);
		}


	}
}
